/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "../Utils/StringBuilder.h"
#include "../IO/PrintStream.h"

namespace Hogshead
{
	namespace Common
	{
		template<typename T> class Table
		{
		private:
			Table()
			{

			}
		public:
			/**
			* Public constructor for Table datastructure.  It defines the initial number of rows and
			* the inital width of each row.  There are initially zero rows allocated, but there are slots
			* for them in the future.
			* @param row_space The number of initial slots available in memory for rows.
			* @param initial_row_width The size of each row when initially created
			*/
			Table(int row_space, int initial_row_width)
			{
				// no rows by default
				_rows = 0;
				// space for row_space 
				_allocated_rows = row_space;
				// width of each row by default
				_initial_row_width = initial_row_width;

				// list of rows
				_data = new T*[_allocated_rows];
				// elements set to 0 initially
				_elements = new int[_allocated_rows];
				// width of each row
				_widths = new int[_allocated_rows];
			}

			Table(const Table& other)
			{
				error("This method should not be used");
			}

			~Table()
			{
				for(int k = 0; k < _rows; k++)
					delete[] _data[k];
				delete[] _data;
				delete[] _elements;
				delete[] _widths;
			}

			Table& operator=(const Table& other)
			{
				error ("This method should not be used");
				return *this;
			}
			/**
			* Returns the number of rows in this table (0 until rows are added)
			*/
			int rows() const
			{
				return _rows;
			}

			/**
			* Returns the number of elements in the given row.
			* @param in_row The row to lookup the number of elements in
			*/
			int width(int in_row) const
			{
				if(in_row >= 0 && in_row < _rows)
					return _elements[in_row];
				error(StringBuilder() << "Table only has " << _rows << " rows");
				return 0;
			}

			/**
			* Returns the pointer to the given row
			* @param in_row The row to retrieve
			* @return The raw buffer data pointer
			*/
			T* operator[](int in_row)
			{
				if(in_row >= 0 && in_row < _rows)
					return _data[in_row];
				error(StringBuilder() << "Table only has " << _rows << " rows");
				return NULL;
			}
			/**
			* Returns the pointer to the given row
			* @param in_row The row to retrieve
			* @return The raw buffer data pointer
			*/
			const T* operator[](int in_row) const
			{
				if(in_row >= 0 && in_row < _rows)
					return _data[in_row];
				error(StringBuilder() << "Table only has " << _rows << " rows");
				return NULL;
			}

			/**
			* Add a T to the table at the end of the given row.
			* @param in_row The row to add the pass in value to
			* @param in_t The data to add to the given row
			**/
			void add(int in_row, T in_t)
			{
				if(in_row >= 0 && in_row < _rows)
				{
					// increase size of row
					if(_widths[in_row] <= _elements[in_row])
					{
						T* new_row = new T[_widths[in_row] * 2];
						for(int k = 0; k < _widths[in_row]; k++)
							new_row[k] = _data[in_row][k];
						_widths[in_row] *= 2;
						delete[] _data[in_row];
						_data[in_row] = new_row;
					}
					_data[in_row][_elements[in_row]++] = in_t;
				}
				else
					error(StringBuilder() << "Table only has " << _rows << " rows");
			}

			/**
			* Add an empty row to the table
			*/
			void addRow()
			{
				if(_allocated_rows <= _rows)
				{
					T** new_data = new T*[_allocated_rows*2];
					int* new_widths = new int[_allocated_rows*2];
					int* new_elements = new int[_allocated_rows*2];
					memcpy(new_data, _data, sizeof(T) * _allocated_rows);
					memcpy(new_widths, _widths, sizeof(int) * _allocated_rows);
					memcpy(new_elements, _elements, sizeof(int) * _allocated_rows);
					
					delete[] _data;
					delete[] _widths;
					delete[] _elements;

					_widths = new_widths;
					_elements = new_elements;
					_data = new_data;

					_allocated_rows *= 2;
				}
				_data[_rows] = new T[_initial_row_width];
				_widths[_rows] = _initial_row_width;
				_elements[_rows] = 0;
				_rows++;
			}

			/**
			* PrintStream print operator
			*/
			friend PrintStream& operator<<(PrintStream& in_stream, const Table& in_table)
			{
				for(int k = 0; k < in_table.rows(); k++)
				{
					const T* row = in_table[k];
					for(int j = 0; j < in_table.width(k); j++)
						in_stream << row[j] << ",";
					in_stream << endl;
				}

				return in_stream;
			}

			/**
			* Remove reference to all elements in the table
			*/
			void clear()
			{
				memset(_elements, 0, sizeof(int) * _rows);
			}

			/**
			* Removes references to all elements below (inclusive) this index
			*/
			void clearBelow(int index)
			{
				memset(_elements + index, 0, sizeof(int) * (_rows - index));
			}

		private:
			// our raw data
			T** _data;
			// the number of rows
			int _rows;
			// number of slots of rows
			int _allocated_rows;
			// the allocated slots in each row
			int* _widths;
			// the number of used slots in each row
			int* _elements;
			// the initial row width
			int _initial_row_width;
		};
	}
}
